﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Security;
using agsXMPP;
using agsXMPP.Xml.Dom;
using agsXMPP.sasl;
using agsXMPP.net;
using agsXMPP.protocol.client;
using agsXMPP.Xml;

namespace testClient {
    public partial class frmMain : Form {
        private XmppClientConnection conn = null;
        public frmMain() {
            InitializeComponent();
        }

        private void initConnEvents(XmppClientConnection c) {
            // 读取接收到的XML流信息
            c.OnReadXml += new XmlHandler(XmppConnectionEvents.onReadXml);
            //向服务器发送XML流信息
            c.OnWriteXml += new XmlHandler(XmppConnectionEvents.onWriteXml);

            //收到Presence节时引发的事件
            c.OnPresence += new PresenceHandler(XmppConnectionEvents.onPresence);
            //收到Message节时引发的事件
            c.OnMessage += new MessageHandler(XmppConnectionEvents.onMessage);
            //收到IQ节时引发的事件
            c.OnIq += new IqHandler(XmppConnectionEvents.onIq);

            c.OnRosterStart += new ObjectHandler(XmppConnectionEvents.onRosterStart);
            c.OnRosterEnd += new ObjectHandler(XmppConnectionEvents.onRosterEnd);
            c.OnRosterItem += new agsXMPP.XmppClientConnection.RosterHandler(XmppConnectionEvents.onRosterItem);

            c.OnAgentStart += new ObjectHandler(XmppConnectionEvents.onAgentStart);
            c.OnAgentEnd += new ObjectHandler(XmppConnectionEvents.onAgentEnd);
            c.OnAgentItem += new XmppClientConnection.AgentHandler(XmppConnectionEvents.onAgentItem);

            //用户登录事件
            c.OnLogin += new ObjectHandler(XmppConnectionEvents.onLogin);
            //关闭连接事件
            c.OnClose += new ObjectHandler(XmppConnectionEvents.onClose);
            //连接错误事件
            c.OnError += new ErrorHandler(XmppConnectionEvents.onError);
            //认证错误事件
            c.OnAuthError += new XmppElementHandler(XmppConnectionEvents.onAuthError);

            //读取Socket事件
            c.OnReadSocketData += new BaseSocket.OnSocketDataHandler(XmppConnectionEvents.onSocketReceive);
            //写入Socket事件
            c.OnWriteSocketData += new BaseSocket.OnSocketDataHandler(XmppConnectionEvents.onSocketSend);

            c.ClientSocket.OnValidateCertificate += new RemoteCertificateValidationCallback(XmppConnectionEvents.onSocketValidateCertificate);

            //连接状态改变事件            
            c.OnXmppConnectionStateChanged += new XmppConnectionStateHandler(XmppConnectionEvents.onStateChanged);
            c.OnSaslStart += new SaslEventHandler(XmppConnectionEvents.onSaslStart);
        }

        private XmppClientConnection initConnection() {
            XmppClientConnection c;
            if (this.rHttp.Checked)
                c = new XmppClientConnection(SocketConnectionType.HttpPolling);
            else
                c = new XmppClientConnection(SocketConnectionType.Direct);
            c.ClientLanguage = "zh-cn";
            c.Server = this.eServer.Text;
            c.Port = int.Parse(this.ePort.Text);
            c.Username = eUser.Text + "@" + eDomain.Text;
            c.Password = ePass.Text;
            return c;
        }

        private void bConnect_Click(object sender, EventArgs e) {
            conn = initConnection();
            initConnEvents(conn);
            conn.Open();
        }
    }
}